<template>
  <div class="p-2">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'project:c_invoice_task:add'"  @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button  type="primary" v-auth="'project:c_invoice_task:exportXls'" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button  type="primary" v-auth="'project:c_invoice_task:importExcel'"  preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <j-upload  @success="handleUploadSuccess" text="导入"  v-model:fileList="fileList" ></j-upload>
		
     
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template> 
      <template v-slot:bodyCell="{ column, record, index, text }">
		  <template v-if="column.key === 'btn'">
		  		<div v-if="record.status === '待审核' && sysUser.roleCode.includes('财务')" > 
		  			<a-button color="warning" @click="handleDetail(record)">审核</a-button>
		  		</div>
				<div v-else >
					<a-button color="success" @click="handleDetail(record)">查看</a-button>
				</div>
		  </template>
		  
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <CInvoiceTaskModal ref="registerModal" @success="handleSuccess"></CInvoiceTaskModal>
  </div>
</template>

<script lang="ts" name="project-cInvoiceTask" setup>
  import { ref, reactive,onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, superQuerySchema } from './CInvoiceTask.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl,getOcrAssistant } from './CInvoiceTask.api';
  import {getLoginUser} from '../common.api.ts'
  import { downloadFile } from '/@/utils/common/renderUtils';
  import CInvoiceTaskModal from './components/CInvoiceTaskModal.vue'
  import { useUserStore } from '/@/store/modules/user';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  
  
  const formRef = ref();
  const queryParam = reactive<any>({});
  const toggleSearchStatus = ref<boolean>(false);
  const registerModal = ref();
  const userStore = useUserStore();
  const sysUser = ref({ roleCode: '' });
  const fileList = ref([]);
  
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '开票任务表',
      api: list,
      columns,
      canResize:false,
      useSearchForm: false,
      beforeFetch: async (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: "开票任务表",
      url: getExportUrl,
      params: queryParam,
    },
	  importConfig: {
	    url: getImportUrl,
	    success: handleSuccess
	  },
  });
  const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;
  const labelCol = reactive({
    xs:24,
    sm:4,
    xl:6,
    xxl:4
  });
  const wrapperCol = reactive({
    xs: 24,
    sm: 20,
  });

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    searchQuery();
  }

  /**
   * 新增事件
   */
  function handleAdd() {
	registerModal.value.disableSubmitOk = false;
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }
  
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
	registerModal.value.disableSubmitOk = true;
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }
   
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
	registerModal.value.disableSubmitOk = true;
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }
   
  
   

   
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
   
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
	  {
	    label: '编辑',
	    onClick: handleEdit.bind(null, record),
	  }
    ];
  }
   

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }
  
  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }
  
  async function getUserData(){
	  sysUser.value = await getLoginUser();
  }
  
  
  async function handleUploadSuccess(response: any, file: any) {
	 
	  const param = {'filePath':response.message}
	  const result = await getOcrAssistant(param);
	   if (result && result.length > 0) {
		   fileList.value.splice(0, 1);
		   
		   
		   
		   
		   
		   // registerModal.value.disableSubmitOk = false;
		   // registerModal.value.disableSubmit = false;
		   // registerModal.value.add();
	   }
	}

 onMounted(() => {
    getUserData()
  });


</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
    .ant-form-item:not(.ant-form-item-with-help){
      margin-bottom: 16px;
      height: 32px;
    }
    :deep(.ant-picker),:deep(.ant-input-number){
      width: 100%;
    }
  }
</style>
